import java.util.ArrayList;
import java.util.List;

/**
 * @author zhaoren
 * @version 1.0
 * @description TODO
 * @date 2020/8/10 15:51
 **/
public class Composite extends Component{

    /**
     * 孩子节点列表
     */
    private final List<Component> childrens = new ArrayList<Component>();

    public Composite(String name) {
        super(name);
    }

    @Override
    public void add(Component component) {
        childrens.add(component);
    }

    @Override
    public void remove(Component component) {
        childrens.remove(component);
    }

    @Override
    public void display(int depth) {
        for (int i = 0; i < depth; i++) {
            System.out.print("-");
        }
        System.out.println(name);
        //遍历其子节点
        childrens.forEach(x ->{
            x.display(depth + 1);
        });
    }
}
